package com.topscomm.pm;

import com.topscomm.basic.BasicEntity;
import com.topscomm.cbo.pojo.CboApprovalNodeEntity;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.cbo.service.ICboApprovalNodeService;
import com.topscomm.mis.MisScript;
import com.topscomm.pm.pojo.PmProjectEntity;
import com.topscomm.pm.service.impl.PmProjectService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.DateUtil;
import com.topscomm.pub.util.I18nUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.tap.ITapDao;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.TapScript;
import com.topscomm.tap.TapService;
import com.topscomm.tap.common.DatacodeCache;
import com.topscomm.tap.common.ParameterCache;
import com.topscomm.tap.common.TapSystemConst.TapApprovalNodeStateEnum;

/**
 * @author: fanzhiming
 * @date: 2017年3月9日下午3:20:41
 * @description:
 * @modify:
 */
public class PmScript extends MisScript {

    /**
     * 生成App消息推送内容
     *
     * @author: lanjianwei
     * @date: 2019年11月12日
     * @params:
     * @return_type: String
     * @description:
     * @modify:
     */
    public String geneAppContentSubmitNew(ITapService service, TapEntity entity) {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        // 组织系统消息内容
        StringBufferProxy msg = new StringBufferProxy();
        DatacodeCache datacodeCache = (DatacodeCache) SpringUtil.getBean("datacodeCache");
        String dispName = datacodeCache
            .getValueDynamic("CboApprovalFlowCategory", nodeins.getSourcetype(), "code", "name");
        String maintitle = nodeins.getMaintitle();
        String extendfield2 = nodeins.getExtendfield2();
        //maintitle只取时间字段
        if (!StringUtil.isEmpty(maintitle)) {
            int pos = maintitle.indexOf("于20");
            if (pos != -1) {
                maintitle = maintitle.substring(pos + 1, pos + 11);
            }
        }
        if (StringUtil.isEmpty(dispName)) {
            dispName = "业务申请";
        }
        if (nodeins.getState() == TapApprovalNodeStateEnum.UnApproved) {
            msg.appendSingle("您于{0}提交的{1}单据在“{2}”阶段被驳回！", maintitle, dispName, extendfield2);
        } else {
            msg.appendSingle("您于{0}提交的{1}单据在“{2}”阶段审批通过！", maintitle, dispName, extendfield2);
        }
        return msg.toString();
    }

    /**
     * 生成手机端系统消息主题
     *
     * @author: lanjianwei
     * @date: 2019年11月12日
     * @params:
     * @return_type: String
     * @description:
     * @modify:
     */
    public String geneSysSubjectSubmitNew(ITapService service, TapEntity entity) {
        StringBufferProxy msg = new StringBufferProxy();
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        DatacodeCache datacodeCache = (DatacodeCache) SpringUtil.getBean("datacodeCache");
        String dispName = datacodeCache
            .getValueDynamic("CboApprovalFlowCategory", nodeins.getSourcetype(), "code", "name");
        if (StringUtil.isEmpty(dispName)) {
            dispName = "业务申请";
        }
        msg.appendSingle("{0}通知", dispName);
        return msg.toString();
    }

    /**
     * 生成手机端系统消息内容
     *
     * @author: lanjianwei
     * @date: 2019年11月12日
     * @params:
     * @return_type: String
     * @description:
     * @modify:
     */
    public String geneSysContentSubmitNew(ITapService service, TapEntity entity) {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        // 组织系统消息内容
        StringBufferProxy msg = new StringBufferProxy();
        DatacodeCache datacodeCache = (DatacodeCache) SpringUtil.getBean("datacodeCache");
        String dispName = datacodeCache
            .getValueDynamic("CboApprovalFlowCategory", nodeins.getSourcetype(), "code", "name");
        String maintitle = nodeins.getMaintitle();
        String extendfield2 = nodeins.getExtendfield2();
        //maintitle只取时间字段
        if (!StringUtil.isEmpty(maintitle)) {
            int pos = maintitle.indexOf("于20");
            if (pos != -1) {
                maintitle = maintitle.substring(pos + 1, pos + 11);
            }
        }
        if (StringUtil.isEmpty(dispName)) {
            dispName = "业务申请";
        }
        if (nodeins.getState() == TapApprovalNodeStateEnum.UnApproved) {
            msg.appendSingle("&nbsp;&nbsp;&nbsp;&nbsp;" + "您于{0}提交的{1}单据在“{2}”阶段被驳回！" + "<br>",
                maintitle, dispName, extendfield2);
        } else {
            msg.appendSingle("&nbsp;&nbsp;&nbsp;&nbsp;" + "您于{0}提交的{1}单据在“{2}”阶段审批通过！" + "<br>",
                maintitle, dispName, extendfield2);
        }
        String approvalopition = nodeins.getApprovalopinion();
        if (StringUtil.isEmpty(approvalopition)) {
            approvalopition = I18nUtil.getMessage("tap.common.not");
        }
        msg.appendSingle(
            "&nbsp;&nbsp;&nbsp;&nbsp;" + I18nUtil.getMessage("tap.cboapprovalnode.approvalcomments")
                + ":{0}<br>", approvalopition);
        msg.appendSingle(
            "&nbsp;&nbsp;&nbsp;&nbsp;" + I18nUtil.getMessage("tap.date.senddate") + ":{0}<br>",
            DateUtil.Now());
        return msg.toString();
    }

    /**
     * @author: wudaliang
     * @date: 2021-01-20
     * @param: service
     * @param: entity
     * @description:
     * @return: java.lang.String
     * @modify:
     */
    public String geneMailContentEnd(ITapService<TapEntity, ITapDao<TapEntity>> service,
        TapEntity entity) {
        ICboApprovalNodeService cboApprovalNodeService = (ICboApprovalNodeService) SpringUtil
            .getBean("cboApprovalNodeService");
        //组织系统消息内容
        StringBufferProxy msg = new StringBufferProxy();
        //获取到对应模块的Service
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourcetype = nodeins.getSourcetype();
        String lowerFirst = sourcetype.substring(0, 1).toLowerCase();
        String sourcetypeNew = lowerFirst + sourcetype.substring(1);
        String serviceName = sourcetypeNew + "Service";
        String entityName = sourcetype + "Entity";
        TapService serviceBean = (TapService) SpringUtil.getBean(serviceName);
        BasicEntity selectEntity = serviceBean
            .queryById(Long.toString(nodeins.getSourceid()));
        PmProjectService pmProjectService = (PmProjectService) SpringUtil
            .getBean("pmProjectService");
        long projectid = 0;
        if (entityName.equals("PmProjectEntity")) {
            projectid = selectEntity.getId();
        } else if (entityName.equals("PmProjectChangeEntity")) {
            // PmProjectChangeEntity实体类里的projectid比较特殊，为string类型,其他实体类的projectid为long类型
            projectid = Long.parseLong(selectEntity.getValue("projectid").toString());
        } else {
            projectid = (long) selectEntity.getValue("projectid");
        }
        PmProjectEntity pmProjectEntity = pmProjectService
            .queryById(ConvertUtil.convertToString(projectid));
        DatacodeCache datacodeCache = (DatacodeCache) SpringUtil.getBean("datacodeCache");
        String dispName = datacodeCache
            .getValueDynamic("CboApprovalFlowCategory", nodeins.getSourcetype(), "code", "name");
        if (StringUtil.isEmpty(dispName)) {
            dispName = "业务申请";
        }
        if (nodeins.getState() == TapApprovalNodeStateEnum.Approved) {
            msg.appendSingle("&nbsp;&nbsp;&nbsp;&nbsp;" + "{0}模块有新的单据审批通过，项目名称是：{1}，项目编号是：{2}",
                dispName, pmProjectEntity.getName(), pmProjectEntity.getCode());
        } else {
            msg.appendSingle("&nbsp;&nbsp;&nbsp;&nbsp;" + "单据未审批通过，请联系系统管理员");
        }
        return msg.toString();
    }
}
